<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/img/iconfont.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/img/iconfont.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/img/iconfont.png">
  <link rel="mask-icon" href="/img/iconfont.png" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/7.0.0/css/all.min.css" integrity="sha256-VHqXKFhhMxcpubYf9xiWdCiojEbY9NexQ4jh8AxbvcM=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"wang-weijun.github.io","root":"/","images":"/images","scheme":"Pisces","darkmode":true,"version":"8.26.0","exturl":false,"sidebar":{"position":"left","width_expanded":320,"width_dual_column":240,"display":"post","padding":18,"offset":12},"hljswrap":true,"codeblock":{"theme":{"light":"default","dark":"stackoverflow-dark"},"prism":{"light":"prism","dark":"prism-dark"},"copy_button":{"enable":false,"style":null},"fold":{"enable":false,"height":500},"language":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"duration":200,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.json","localsearch":{"enable":true,"top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js" defer></script>

    <meta name="description" content="深度学习记录">
<meta property="og:type" content="article">
<meta property="og:title" content="深度学习">
<meta property="og:url" content="http://wang-weijun.github.io/2023/05/17/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/index.html">
<meta property="og:site_name" content="Phils的杂货铺">
<meta property="og:description" content="深度学习记录">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://wang-weijun.github.io/images/202310250928177.png">
<meta property="og:image" content="http://wang-weijun.github.io/images/202310250929960.png">
<meta property="og:image" content="http://wang-weijun.github.io/images/202310250929545.png">
<meta property="og:image" content="http://wang-weijun.github.io/images/202310250929628.png">
<meta property="og:image" content="http://wang-weijun.github.io/images/202310250930975.png">
<meta property="og:image" content="http://wang-weijun.github.io/images/202310250930717.png">
<meta property="og:image" content="http://wang-weijun.github.io/images/202310250932410.png">
<meta property="og:image" content="http://wang-weijun.github.io/images/202310250933550.png">
<meta property="og:image" content="http://wang-weijun.github.io/images/202310250933172.png">
<meta property="og:image" content="http://wang-weijun.github.io/images/202310250934165.png">
<meta property="og:image" content="http://wang-weijun.github.io/images/202310250934607.png">
<meta property="og:image" content="http://wang-weijun.github.io/images/202310250935732.png">
<meta property="og:image" content="http://wang-weijun.github.io/images/image-20230509030441465.png">
<meta property="article:published_time" content="2023-05-17T08:04:33.000Z">
<meta property="article:modified_time" content="2025-11-21T08:58:26.679Z">
<meta property="article:author" content="Phils">
<meta property="article:tag" content="Torch">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://wang-weijun.github.io/images/202310250928177.png">


<link rel="canonical" href="http://wang-weijun.github.io/2023/05/17/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">


<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://wang-weijun.github.io/2023/05/17/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/","path":"2023/05/17/深度学习/","title":"深度学习"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>深度学习 | Phils的杂货铺</title>
  








  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous" defer></script>
<script src="/js/utils.js" defer></script><script src="/js/motion.js" defer></script><script src="/js/sidebar.js" defer></script><script src="/js/next-boot.js" defer></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.5.0/search.js" integrity="sha256-xFC6PJ82SL9b3WkGjFavNiA9gm5z6UBxWPiu4CYjptg=" crossorigin="anonymous" defer></script>
<script src="/js/third-party/search/local-search.js" defer></script>







  





  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Phils的杂货铺</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
      <div class="search-header">
        <span class="search-icon">
          <i class="fa fa-search"></i>
        </span>
        <div class="search-input-container">
          <input autocomplete="off" autocapitalize="off" maxlength="80"
                placeholder="搜索..." spellcheck="false"
                type="search" class="search-input">
        </div>
        <span class="popup-btn-close" role="button">
          <i class="fa fa-times-circle"></i>
        </span>
      </div>
      <div class="search-result-container">
        <div class="search-result-icon">
          <i class="fa fa-spinner fa-pulse fa-5x"></i>
        </div>
      </div>
    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B5%81%E7%A8%8B"><span class="nav-number">1.</span> <span class="nav-text">流程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#pycharm"><span class="nav-number">2.</span> <span class="nav-text">pycharm</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#conda-%E7%8E%AF%E5%A2%83"><span class="nav-number">2.1.</span> <span class="nav-text">conda 环境</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#conda-%E5%85%B6%E4%BB%96%E5%91%BD%E4%BB%A4"><span class="nav-number">2.2.</span> <span class="nav-text">conda 其他命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B8%B8%E8%A7%81%E6%8A%A5%E9%94%99"><span class="nav-number">2.3.</span> <span class="nav-text">常见报错</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8A%A8%E6%89%8B%E5%AD%A6%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0"><span class="nav-number">3.</span> <span class="nav-text">动手学深度学习</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%90%8D%E8%AF%8D%E8%A7%A3%E9%87%8A"><span class="nav-number">4.</span> <span class="nav-text">名词解释</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9C%89%E7%9B%91%E7%9D%A3%E5%AD%A6%E4%B9%A0"><span class="nav-number">4.1.</span> <span class="nav-text">有监督学习</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%97%A0%E7%9B%91%E7%9D%A3%E5%AD%A6%E4%B9%A0"><span class="nav-number">4.2.</span> <span class="nav-text">无监督学习</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92%E4%B8%8E%E9%80%BB%E8%BE%91%E5%9B%9E%E5%BD%92"><span class="nav-number">5.</span> <span class="nav-text">线性回归与逻辑回归</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%94%AF%E6%8C%81%E5%90%91%E9%87%8F%E6%9C%BA-SVM"><span class="nav-number">6.</span> <span class="nav-text">支持向量机 SVM</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Softmax-%E5%9B%9E%E5%BD%92"><span class="nav-number">7.</span> <span class="nav-text">Softmax 回归</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9D%87%E6%96%B9%E6%8D%9F%E5%A4%B1"><span class="nav-number">7.1.</span> <span class="nav-text">均方损失</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Softmax-%E5%92%8C%E4%BA%A4%E5%8F%89%E7%86%B5%E6%8D%9F%E5%A4%B1"><span class="nav-number">7.2.</span> <span class="nav-text">Softmax 和交叉熵损失</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BB%8E%E9%9B%B6%E5%BC%80%E5%A7%8B%E5%AE%9E%E7%8E%B0"><span class="nav-number">7.3.</span> <span class="nav-text">从零开始实现</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8D%9F%E5%A4%B1%E5%87%BD%E6%95%B0"><span class="nav-number">8.</span> <span class="nav-text">损失函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9B%BE%E5%83%8F%E5%88%86%E7%B1%BB%E6%95%B0%E6%8D%AE%E9%9B%86"><span class="nav-number">9.</span> <span class="nav-text">图像分类数据集</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%84%9F%E7%9F%A5%E6%9C%BA"><span class="nav-number">10.</span> <span class="nav-text">感知机</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%9A%E5%B1%82%E6%84%9F%E7%9F%A5%E6%9C%BA"><span class="nav-number">10.1.</span> <span class="nav-text">多层感知机</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%A8%A1%E5%9E%8B%E9%80%89%E6%8B%A9"><span class="nav-number">11.</span> <span class="nav-text">模型选择</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#K-%E6%8A%98%E4%BA%A4%E5%8F%89%E9%AA%8C%E8%AF%81"><span class="nav-number">11.1.</span> <span class="nav-text">K-折交叉验证</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%80%BB%E7%BB%93"><span class="nav-number">11.1.1.</span> <span class="nav-text">总结</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BF%87%E6%8B%9F%E5%90%88%E5%92%8C%E6%AC%A0%E6%8B%9F%E5%90%88"><span class="nav-number">11.2.</span> <span class="nav-text">过拟合和欠拟合</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8D%B7%E7%A7%AF%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C"><span class="nav-number">12.</span> <span class="nav-text">卷积神经网络</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%AD%E8%A8%80%E8%AF%86%E5%88%AB"><span class="nav-number">12.1.</span> <span class="nav-text">语言识别</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Phils"
      src="/img/profile.png">
  <p class="site-author-name" itemprop="name">Phils</p>
  <div class="site-description" itemprop="description">个人博客，IT，技术分享</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">40</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">15</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">40</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/wang-weijun" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wang-weijun" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1191206969@qq.com" title="E-Mail → mailto:1191206969@qq.com" rel="noopener me" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://wang-weijun.github.io/2023/05/17/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/img/profile.png">
      <meta itemprop="name" content="Phils">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Phils的杂货铺">
      <meta itemprop="description" content="个人博客，IT，技术分享">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="深度学习 | Phils的杂货铺">
      <meta itemprop="description" content="深度学习记录">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          深度学习
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-05-17 16:04:33" itemprop="dateCreated datePublished" datetime="2023-05-17T16:04:33+08:00">2023-05-17</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2025-11-21 16:58:26" itemprop="dateModified" datetime="2025-11-21T16:58:26+08:00">2025-11-21</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/" itemprop="url" rel="index"><span itemprop="name">深度学习</span></a>
        </span>
    </span>

  
</div>

            <div class="post-description">深度学习记录</div>
        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><h2 id="流程"><a href="#流程" class="headerlink" title="流程"></a>流程</h2><ul>
<li><strong>数据处理</strong><ol>
<li>读入数据</li>
<li>划分数据集</li>
<li>生成批次数据</li>
<li>训练样本集乱序</li>
<li>校验数据有效性</li>
</ol>
</li>
<li><strong>模型设计</strong><ol>
<li>网络结构</li>
<li>损失函数</li>
</ol>
</li>
<li><strong>训练配置</strong><ol>
<li>优化算法<ul>
<li>随机梯度下降（SGD）：随机梯度下降算法，每次训练少量数据，抽样偏差导致参数收敛过程中震荡。</li>
<li>动量（Momentum）：引入物理“动量”的概念，累积速度，减少震荡，使参数更新的方向更稳定。</li>
<li>AdaGrad： 根据不同参数距离最优解的远近，动态调整学习率。学习率逐渐下降，依据各参数变化大小调整学习率。</li>
<li>Adam： 由于动量和自适应学习率两个优化思路是正交的，因此可以将两个思路结合起来，这就是当前广泛应用的算法。</li>
</ul>
</li>
<li>设置学习率</li>
</ol>
</li>
</ul>
<h2 id="pycharm"><a href="#pycharm" class="headerlink" title="pycharm"></a>pycharm</h2><h3 id="conda-环境"><a href="#conda-环境" class="headerlink" title="conda 环境"></a>conda 环境</h3><blockquote>
<p>新建环境</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">conda create -n yourEnv python=3.10</span><br></pre></td></tr></table></figure>

<ul>
<li>–name：也可以缩写为 【-n】，【yourEnv】是新创建的虚拟环境的名字，创建完，可以装 anaconda 的目录下找到 envs&#x2F;yourEnv 目录</li>
<li>python&#x3D;2.7：是 python 的版本号。也可以指定为【python&#x3D;3.6】，若未指定，默认为是装 anaconda 时 python 的版本.</li>
</ul>
<blockquote>
<p>激活环境</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">activate yourEnv</span><br></pre></td></tr></table></figure>

<ul>
<li><p>windows 用户环境变量中添加（改成自己的路径）：</p>
<blockquote>
<p>D:\Anaconda3<br>D:\Anaconda3\Scripts<br>D:\Anaconda3\Library\bin</p>
</blockquote>
</li>
</ul>
<blockquote>
<p>查看活跃的环境</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">conda info --envs</span><br></pre></td></tr></table></figure>

<p>输出中带有【*】号的的就是当前所处的环境</p>
<p>当&#x3D;&#x3D;创建环境&#x3D;&#x3D;时，出现**<code>NoWritablePkgsDirError: No writeable pkgs directories configured.</code>**报错</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">conda config --add pkgs_dirs D:\Environment\Anaconda3\pkgs</span><br><span class="line">conda config --add envs_dirs D:\Environment\Anaconda3\envs</span><br></pre></td></tr></table></figure>

<h3 id="conda-其他命令"><a href="#conda-其他命令" class="headerlink" title="conda 其他命令"></a>conda 其他命令</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">conda list: 看这个环境下安装的包和版本</span><br><span class="line">conda install numpy scikit-learn: 安装numpy sklearn包</span><br><span class="line">conda <span class="built_in">env</span> remove -n yourEnv: 删除你的环境</span><br><span class="line">conda <span class="built_in">env</span> list: 查看所有的环境</span><br></pre></td></tr></table></figure>

<h3 id="常见报错"><a href="#常见报错" class="headerlink" title="常见报错"></a>常见报错</h3><p>img.imshow（xxx）不显示图片</p>
<p>在 pycharm 中设置图片不在 pycharm 中显示（关闭 Show plots in tool window）</p>
<p><img src="/../images/202310250928177.png" alt="image-20231025092823116"></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pylab</span><br><span class="line"></span><br><span class="line">pylab.show()</span><br></pre></td></tr></table></figure>

<p>当控制台报</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">OMP: Error <span class="comment">#15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.</span></span><br><span class="line">OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to <span class="keyword">do</span> is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime <span class="keyword">in</span> any library. As an unsafe, unsupported, undocumented workaround you can <span class="built_in">set</span> the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to <span class="built_in">continue</span> to execute, but that may cause crashes or silently produce incorrect results. For more information, please see http://www.intel.com/software/products/support/.</span><br></pre></td></tr></table></figure>

<p>使用</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">import os</span><br><span class="line">os.environ[&#x27;KMP_DUPLICATE_LIB_OK&#x27;] = &#x27;True&#x27;</span><br></pre></td></tr></table></figure>

<h2 id="动手学深度学习"><a href="#动手学深度学习" class="headerlink" title="动手学深度学习"></a>动手学深度学习</h2><p>下载教材</p>
<p><code>幻灯片版</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">https://github.com/d2l-ai/d2l-zh-pytorch-slides.git</span><br><span class="line">git <span class="built_in">clone</span> https://github.com/d2l-ai/d2l-zh-pytorch-slides.git</span><br></pre></td></tr></table></figure>

<h2 id="名词解释"><a href="#名词解释" class="headerlink" title="名词解释"></a>名词解释</h2><h3 id="有监督学习"><a href="#有监督学习" class="headerlink" title="有监督学习"></a>有监督学习</h3><h3 id="无监督学习"><a href="#无监督学习" class="headerlink" title="无监督学习"></a>无监督学习</h3><h2 id="线性回归与逻辑回归"><a href="#线性回归与逻辑回归" class="headerlink" title="线性回归与逻辑回归"></a>线性回归与逻辑回归</h2><ul>
<li>线性回归是一种用于预测和拟合函数的方法。它用于回归任务，例如用来拟合一条直线。</li>
</ul>
<blockquote>
<p>应用：线性回归常用于连续变量的预测，例如房价预测、股票价格预测等。它可以用来拟合一条直线，描述自变量和因变量之间的关系。</p>
</blockquote>
<ul>
<li>逻辑回归是一种用于分类和预测函数的方法。它用于分类任务，可以进行二分类或多分类。它是在给定自变量和超参数后，得到因变量的期望，基于这个期望来处理预测分类问题。</li>
</ul>
<blockquote>
<p>应用：逻辑回归常用于分类问题，例如垃圾邮件分类、疾病诊断等。它可以用来预测一个事件发生的概率，并根据这个概率进行分类。</p>
</blockquote>
<h2 id="支持向量机-SVM"><a href="#支持向量机-SVM" class="headerlink" title="支持向量机 SVM"></a>支持向量机 SVM</h2><p><strong>线性可分支持向量机</strong></p>
<p>$w&#x3D;(w_1,w_2\cdots w_n)$</p>
<p>$x&#x3D;(x_1,x_2\cdots x_n)$</p>
<p>b &#x3D; -10</p>
<p>$w ×x+b&#x3D;0$</p>
<p><strong>线性支持向量机</strong></p>
<p><strong>非线性支持向量机</strong></p>
<h2 id="Softmax-回归"><a href="#Softmax-回归" class="headerlink" title="Softmax 回归"></a>Softmax 回归</h2><blockquote>
<p>区别</p>
</blockquote>
<p><img src="/../images/202310250929960.png" alt="image-20231025092900871"></p>
<h3 id="均方损失"><a href="#均方损失" class="headerlink" title="均方损失"></a>均方损失</h3><p><img src="/../images/202310250929545.png" alt="image-20231025092933504"></p>
<h3 id="Softmax-和交叉熵损失"><a href="#Softmax-和交叉熵损失" class="headerlink" title="Softmax 和交叉熵损失"></a>Softmax 和交叉熵损失</h3><ul>
<li>交叉熵常用来衡量两个概率的区别</li>
</ul>
<p><img src="/../images/202310250929628.png" alt="image-20231025092956599"></p>
<p>总结：</p>
<ul>
<li>Softmax 回归是一个多分类模型</li>
<li>使用 Softmax 操作子得到每个类的预测置信度</li>
<li>使用交叉熵来衡量预测和标号的区别作为损失函数</li>
</ul>
<h3 id="从零开始实现"><a href="#从零开始实现" class="headerlink" title="从零开始实现"></a>从零开始实现</h3><p>$softmax(X)<em>{ij}&#x3D;\frac{exp(X</em>{ij})}{∑<em>kexp(X</em>{ik})}$</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">softmax</span>(<span class="params">X</span>):</span><br><span class="line">    X_exp = torch.exp(X)</span><br><span class="line">    partition = X_exp.<span class="built_in">sum</span>(<span class="number">1</span>, keepdim=<span class="literal">True</span>)</span><br><span class="line">    <span class="keyword">return</span> X_exp / partition <span class="comment"># 应用广播机制</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>实现结果</p>
</blockquote>
<p><img src="/../images/202310250930975.png" alt="image-20231025093023908"></p>
<h2 id="损失函数"><a href="#损失函数" class="headerlink" title="损失函数"></a>损失函数</h2><p>L2 Loss（均方损失）</p>
<blockquote>
<p>$l(y,y’)&#x3D;\frac{1}{2}(y-y’)^2$</p>
</blockquote>
<p>L1 Loss</p>
<blockquote>
<p>$l(y,y’)&#x3D;|y-y’|$</p>
</blockquote>
<h2 id="图像分类数据集"><a href="#图像分类数据集" class="headerlink" title="图像分类数据集"></a>图像分类数据集</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> torch</span><br><span class="line"><span class="keyword">from</span> torch.utils <span class="keyword">import</span> data</span><br><span class="line"><span class="keyword">from</span> d2l <span class="keyword">import</span> torch <span class="keyword">as</span> d2l</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">get_dataloader_workers</span>():</span><br><span class="line">    <span class="string">&quot;&quot;&quot;使用4个进程来读取数据。&quot;&quot;&quot;</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">4</span></span><br><span class="line"></span><br><span class="line">train_iter = data.DataLoader(mnist_train, batch_size, shuffle=<span class="literal">True</span>, num_workers=get_dataloader_workers())</span><br><span class="line"></span><br><span class="line">timer = d2l.Timer()</span><br><span class="line"><span class="keyword">for</span> X, y <span class="keyword">in</span> train_iter:</span><br><span class="line">    <span class="keyword">continue</span></span><br><span class="line"><span class="comment"># 打印读取数据时间</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&#x27;<span class="subst">&#123;timer.stop():<span class="number">.2</span>f&#125;</span> sec&#x27;</span>)</span><br></pre></td></tr></table></figure>

<h2 id="感知机"><a href="#感知机" class="headerlink" title="感知机"></a>感知机</h2><p><strong>二分类问题</strong></p>
<ul>
<li><p>给定输入 x，权重 w，和偏移 b，感知机输出：</p>
<p>$\omicron&#x3D;\sigma(&lt;w,x&gt;+b)$ $\sigma(x)&#x3D;\begin{cases} 1，if \quad x&gt;0 \ -1， otherwise  \end{cases}$</p>
</li>
<li><p>二分类：-1 或 1</p>
<ul>
<li>Vs.回归输出实数</li>
<li>Vs.Softmax 回归输出概率</li>
</ul>
</li>
</ul>
<p><img src="/../images/202310250930717.png" alt="image-20231025093059675"></p>
<p><strong>收敛定理</strong></p>
<ul>
<li>数据在半径<strong>r</strong>内</li>
<li>余量$\rho$分类两类<ul>
<li>y($x^T$w+b) $\geqslant$ $\rho$</li>
</ul>
</li>
</ul>
<p> 对于$||W||^2+b^2\leqslant1$</p>
<ul>
<li>感知机保证在$\frac {r^2+1}{\rho^2}$步后收敛</li>
</ul>
<p><strong>XOR 问题</strong></p>
<p>感知机不能拟合 XOR 函数，它只能产生线性分割面</p>
<h3 id="多层感知机"><a href="#多层感知机" class="headerlink" title="多层感知机"></a>多层感知机</h3><p><img src="/../images/202310250932410.png" alt="image-20231025093225363"></p>
<ul>
<li><p>X，Y 先进入蓝色进行判断：（1，3）为+，（2，4）为－</p>
</li>
<li><p>X，Y 再进入黄色进行判断：（1，2）为+，（3，4）为-</p>
</li>
<li><p>product 对前面的结果做乘积，相同为+ ，相异为-</p>
</li>
</ul>
<p><strong>总结</strong></p>
<ul>
<li>多层感知器使用隐藏层和激活函数来得到非线性模型</li>
<li>常用激活函数是 sigmoid，Tanh，ReLU</li>
<li>使用 softmax 来处理多类分类</li>
<li>超参数为隐藏层数，和各个隐藏层大小</li>
</ul>
<blockquote>
<p>代码</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> torch</span><br><span class="line"><span class="keyword">from</span> torch <span class="keyword">import</span> nn</span><br><span class="line"><span class="keyword">from</span> d2l <span class="keyword">import</span> torch <span class="keyword">as</span> d2l</span><br><span class="line"><span class="keyword">import</span> pylab</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"></span><br><span class="line">os.environ[<span class="string">&#x27;KMP_DUPLICATE_LIB_OK&#x27;</span>] = <span class="string">&#x27;True&#x27;</span></span><br><span class="line"><span class="comment"># 批量大小</span></span><br><span class="line">batch_size = <span class="number">64</span></span><br><span class="line">train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)</span><br><span class="line"><span class="comment"># 超参数</span></span><br><span class="line">num_inputs, num_outputs, num_hiddens = <span class="number">784</span>, <span class="number">10</span>, <span class="number">256</span></span><br><span class="line">W1 = nn.Parameter(torch.randn(num_inputs, num_hiddens, requires_grad=<span class="literal">True</span>) * <span class="number">0.01</span>)</span><br><span class="line">b1 = nn.Parameter(torch.zeros(num_hiddens, requires_grad=<span class="literal">True</span>))</span><br><span class="line">W2 = nn.Parameter(torch.randn(num_hiddens, num_outputs, requires_grad=<span class="literal">True</span>) * <span class="number">0.01</span>)</span><br><span class="line">b2 = nn.Parameter(torch.zeros(num_outputs, requires_grad=<span class="literal">True</span>))</span><br><span class="line">params = [W1, b1, W2, b2]</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">relu</span>(<span class="params">X</span>):</span><br><span class="line">    a = torch.zeros_like(X)</span><br><span class="line">    <span class="keyword">return</span> torch.<span class="built_in">max</span>(X, a)</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">net</span>(<span class="params">X</span>):</span><br><span class="line">    X = X.reshape((-<span class="number">1</span>, num_inputs))</span><br><span class="line">    H = relu(X @ W1 + b1)</span><br><span class="line">    <span class="keyword">return</span> (H @ W2 + b2)</span><br><span class="line"></span><br><span class="line">loss = nn.CrossEntropyLoss()</span><br><span class="line">num_epochs, lr = <span class="number">10</span>, <span class="number">0.1</span></span><br><span class="line">updater = torch.optim.SGD(params, lr=lr)</span><br><span class="line">d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, updater)</span><br><span class="line">d2l.predict_ch3(net, test_iter)</span><br><span class="line">pylab.show()</span><br></pre></td></tr></table></figure>

<blockquote>
<p>结果</p>
</blockquote>
<p><img src="/../images/202310250933550.png" alt="image-20231025093333510"></p>
<h2 id="模型选择"><a href="#模型选择" class="headerlink" title="模型选择"></a>模型选择</h2><ul>
<li><p>训练误差：模型在训练数据上的误差</p>
</li>
<li><p>泛化误差：模型在新数据上的误差</p>
</li>
<li><p>验证数据集：评估模型好坏的数据集（不要和测试数据集混在一起）</p>
</li>
<li><p>测试数据集：只使用一次（例如竞赛排行榜的数据集，只使用一次）</p>
</li>
</ul>
<h3 id="K-折交叉验证"><a href="#K-折交叉验证" class="headerlink" title="K-折交叉验证"></a><strong>K-折交叉验证</strong></h3><p>将含有<strong>N</strong> 个样本的数据集，分成<strong>K</strong> 份，每份含有 <strong>$\frac{N}{K}$<strong>个样本。选择其中一份作为验证集，另外</strong>K − 1</strong>份作为训练集，验证集集就有<strong>K</strong>种情况。</p>
<ul>
<li>在没有足够多数据时使用</li>
<li>使用<strong>i</strong>块作为验证数据集，其余作为训练集</li>
</ul>
<blockquote>
<p>最后 K 个验证集误差求平均</p>
</blockquote>
<ul>
<li>常用：K &#x3D; 5 或 10（程序跑 5 次或 10 次）</li>
</ul>
<h4 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h4><ul>
<li>训练数据集：训练模型参数</li>
<li>验证数据集：选择模型超参数</li>
<li>非大数据集上通常使用 K-折交叉验证</li>
</ul>
<h3 id="过拟合和欠拟合"><a href="#过拟合和欠拟合" class="headerlink" title="过拟合和欠拟合"></a>过拟合和欠拟合</h3><p><img src="/../images/202310250933172.png" alt="image-20231025093359121"></p>
<table>
<thead>
<tr>
<th align="center">$\frac{数据}{模型容量}$</th>
<th align="center">简单</th>
<th align="center">复杂</th>
</tr>
</thead>
<tbody><tr>
<td align="center">低</td>
<td align="center">正常</td>
<td align="center"><strong>欠拟合</strong></td>
</tr>
<tr>
<td align="center">高</td>
<td align="center"><strong>过拟合</strong></td>
<td align="center">正常</td>
</tr>
</tbody></table>
<p><strong>模型容量的影响</strong></p>
<p><img src="/../images/202310250934165.png" alt="image-20231025093422126"></p>
<p><strong>数据复杂度</strong></p>
<ul>
<li>样本个数</li>
<li>每个样本的元素个数</li>
<li>时间、空间结构</li>
<li>多样性</li>
</ul>
<p><strong>总结</strong></p>
<ul>
<li>模型容量需要匹配数据复杂度，否则可能导致欠拟合和过拟合</li>
<li>统计机器学习提供数学工具来衡量模型复杂度</li>
<li>实际中一般靠观察训练误差和验证误差</li>
</ul>
<blockquote>
<p>结果</p>
</blockquote>
<p>正常</p>
<p><img src="/../images/202310250934607.png" alt="image-20230509030346933.png"></p>
<p>欠拟合</p>
<p><img src="/../images/202310250935732.png" alt="image-20230509030430564.png"></p>
<p>过拟合</p>
<p><img src="/../images/image-20230509030441465.png" alt="image-20230509030441465.png"></p>
<h2 id="卷积神经网络"><a href="#卷积神经网络" class="headerlink" title="卷积神经网络"></a>卷积神经网络</h2><h3 id="语言识别"><a href="#语言识别" class="headerlink" title="语言识别"></a>语言识别</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip install SpeechRecognition</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Torch/" rel="tag"># Torch</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2023/05/17/Markdown%E8%AF%AD%E6%B3%95/" rel="prev" title="Markdown语法">
                  <i class="fa fa-angle-left"></i> Markdown语法
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2023/05/17/Docker/" rel="next" title="Docker">
                  Docker <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Phils</span>
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>

</body>
</html>
